
#include "galaxy_preview.h"


GalaxyPreview::GalaxyPreview(QWidget *parent)
{
  this->parent = parent;
  this->generateStars();
  this->rotation = 0.0f;
  this->nbStarToShow = 2;
}

GalaxyPreview::~GalaxyPreview()
{
//   this->timer->stop();
}

QSize		GalaxyPreview::sizeHint() const
{
  return QSize(400, 400);
}

QSize		GalaxyPreview::minimumSizeHint() const
{
  return QSize(200, 200);
}

void		GalaxyPreview::animate()
{
  this->rotation = this->rotation + 0.5f;
  updateGL();
}

void		GalaxyPreview::changeNbStar(int newNb)
{
  this->nbStarToShow = newNb;
  updateGL();
}

void		GalaxyPreview::initializeGL()
{
  glEnable(GL_TEXTURE_2D);
  // anti aliasing
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_BLEND);
  // for points
  glEnable(GL_POINT_SMOOTH);
  glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
  // for lines
  glEnable(GL_LINE_SMOOTH);
  glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  // for polygons
  glEnable(GL_POLYGON_SMOOTH);
  glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);

  // Black background
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
}

void		GalaxyPreview::paintGL()
{
  StarCoordT	*star;
  int		i;
  
  // Clear the window with current clearing color
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  // Save matrix state and do the rotation
  glPushMatrix();

  glTranslatef(0.0f, 0.0f, -600.0f); 
  //glRotatef(this->rotation, 1.0f, 0.0f, 0.0f);
  glRotatef(this->rotation, 0.0f, 1.0f, 0.0f);
  //   zoom
  glScalef(0.8f, 0.8f, 0.8f);
  
  glShadeModel(GL_SMOOTH);
  //glPushMatrix();
  for(i = 0; i < this->nbStarToShow; i++)
    {
      star = this->stars.at(i);
      // color switch
      switch(star->color)
	{
	case SOL_SYS_COLOR_RED:
	  glColor3f(1.0f, 0.0f, 0.0f);
	  break;
	case SOL_SYS_COLOR_YELLOW:
	  glColor3f(0.937f, 0.898f, 0.546f);
	  break;
	case SOL_SYS_COLOR_WHITE:
	  glColor3f(1.0f, 1.0f, 1.0f);
	  break;
	case  SOL_SYS_COLOR_BLUE:
	  glColor3f(0.117f, 0.562f, 1.0f);
	  break;
	}

      switch(star->size)
	{
	case SOL_SYS_BIG:
	  glPointSize(5);
	  break;
	case SOL_SYS_MEDIUM:
	  glPointSize(3);
	  break;
	case SOL_SYS_SMALL:
	  glPointSize(2);
	  break;
	}
     

      glBegin(GL_POINTS);
      glVertex3f(star->x, star->y, star->z);
      glEnd();
    }
  glColor3f(1.0f, 0.0f, 1.0f);
  glFlush();
}

void		GalaxyPreview::resizeGL(int w, int h)
{
  GLfloat	fAspect;
  
  // Prevent a divide by zero
  if(h == 0)
    h = 1;

  // Set Viewport to window dimensions
  glViewport(0, 0, w, h);

  fAspect = (GLfloat)w/(GLfloat)h;

  // Reset coordinate system
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  gluPerspective(45.0f, fAspect, 1.0, 1000.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void		GalaxyPreview::generateStars()
{
  StarCoordT	*star;
  int		i;

  srand(time(NULL));

  for(i = 0; i < GALAXY_PRE_MAX_SIZE; i++)
    {
      star = new StarCoordT;
      
      if(rand() % 2 == 0)
	star->x = -(rand() % GALAXY_PRE_CUBE_SIZE);
      else
	star->x = rand() % GALAXY_PRE_CUBE_SIZE;
      if(rand() % 2 == 0)
	star->y = -(rand() % GALAXY_PRE_CUBE_SIZE);
      else
	star->y = rand() % GALAXY_PRE_CUBE_SIZE;
      if(rand() % 2 == 0)
	star->z = -(rand() % GALAXY_PRE_CUBE_SIZE);
      else
	star->z = rand() % GALAXY_PRE_CUBE_SIZE;
      star->color = rand() % 4;
      star->size = rand() % 3;
      this->stars.push_back(star);
    }
}
